package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.CheckItemService;
import com.itheima.service.UserManagementService;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户管理
 */

@RestController
@RequestMapping("/user")
public class UserManagement {
    @Reference//查找服务
    private UserManagementService userManagementService;

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user){
        try{
            userManagementService.add(user);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    //用户分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userManagementService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除用户
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            userManagementService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user){
        try{
            userManagementService.edit(user);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            User user = userManagementService.findById(id);
            return  new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<User> list = userManagementService.findAll();
            return  new Result(true, MessageConstant.QUERY_ALLUSER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_ALLUSER_FAIL);
        }
    }
    //查询所有角色
    @RequestMapping("/findAllRole")
    public Result findAllRole(){
    List<Role> roles =  userManagementService.findAllRole();
        /*try{
            List<User> list = userManagementService.findAll();
            return  new Result(true, MessageConstant.QUERY_ALLUSER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_ALLUSER_FAIL);
        }*/
        return new Result(true,"角色信息查询成功",roles);
    }
}
